﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
namespace Benefit.Service.Sys
{
    /// <summary>
    /// 访问权限
    /// </summary>
    public class ServiceVisitPrower : Benefit.Interface.Sys.IVisitPrower
    {
        Benefit.DB.DBManager db = null;
        public ServiceVisitPrower(Benefit.DB.DBManager db)
        {
            this.db = db;
        }

        public bool HasOperatorPrower(int visiterID, int operatorId)
        {
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            bool hasPrower = false;
            //  string s = "访问者ID：" + visiterID + " 被访问者ID：" + operatorId + " 访问者类型：" + cookie.GetCookie("LoginUserType");
            //  s.Debug();
            //   string usertype = cookie.GetCookie("usertype");
            int usertype = Convert.ToInt32(cookie.GetCookie("LoginUserType"));
            List<Benefit.Models.Config.T_MasterGroup> visiterGroups = new List<Benefit.Models.Config.T_MasterGroup>();
            //if (!string.IsNullOrEmpty(usertype))
            //{
            //    visiterGroups = db.MasterGroup.Where(a => a.MasterId == visiterID).Where(a => a.UserType == 2).ToList();
            //}
            //else
            //{
            visiterGroups = db.MasterGroup.Where(a => a.MasterId == visiterID).Where(a => a.UserType == usertype).ToList();
            // }

            foreach (Benefit.Models.Config.T_MasterGroup mg in visiterGroups)
            {
                int groupId = Convert.ToInt32(mg.GroupId);
                var actionlist = db.ActionGroup.Where(a => a.GroupId == groupId).ToList();
                foreach (Benefit.Models.Config.T_ActionGroup a in actionlist)
                {
                    if (a.ActionId.Equals("4") && visiterID == operatorId)//查看自身交易情况
                    {
                        hasPrower = true;
                        break;
                    }

                    if (a.ActionId.Equals("1") || a.ActionId.Equals("3"))//判断是否具有查看全部交易员交易情况和公司全部交易员交易情况
                    {
                        hasPrower = true;
                        break;
                    }

                    //根据被查看的交易员编号是否被配置入查看权限
                    if (a.ActionId.Equals("o " + operatorId))
                    {
                        hasPrower = true;
                        break;
                    }
                    //根据是否具有被查看交易员所在小组的查看权限
                    var operatorGroups = db.GroupOperators.Where(af => af.OperatorId == operatorId).ToList();
                    foreach (Models.Sys.GroupOperators g in operatorGroups)
                    {
                        if (("g " + g.GroupId).Equals(a.ActionId))
                        {
                            hasPrower = true;
                            break;
                        }
                    }
                }
                if (hasPrower)
                {
                    break;
                }
            }
            return hasPrower;
        }

        public bool OperatorHasGroupPrower(int visiterID, int groupId)
        {
            bool hasPrower = false;
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            int usertype = Convert.ToInt32(cookie.GetCookie("LoginUserType"));
            List<Benefit.Models.Config.T_MasterGroup> visiterGroups = new List<Benefit.Models.Config.T_MasterGroup>();

            visiterGroups = db.MasterGroup.Where(a => a.MasterId == visiterID).Where(a => a.UserType == usertype).ToList();
            foreach (Benefit.Models.Config.T_MasterGroup mg in visiterGroups)
            {
                int _groupId = Convert.ToInt32(mg.GroupId);
                var actionlist = db.ActionGroup.Where(a => a.GroupId == _groupId).ToList();
                foreach (Benefit.Models.Config.T_ActionGroup a in actionlist)
                {
                    if (a.ActionId.Equals("2") || a.ActionId.Equals("3"))//判断是否具有查看全部小组交易情况和公司全部交易员交易情况
                    {
                        hasPrower = true;
                        break;
                    }


                    if (("g " + groupId).Equals(a.ActionId))   //根据是否具有被查看交易员所在小组的查看权限
                    {
                        hasPrower = true;
                        break;
                    }
                }
                if (hasPrower)
                {
                    break;
                }
            }
            return hasPrower;
        }

        public bool HasGroupPrower(int visiterID, int groupId)
        {
            bool hasPrower = false;
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            int usertype = Convert.ToInt32(cookie.GetCookie("LoginUserType"));
            List<Benefit.Models.Config.T_MasterGroup> visiterGroups = new List<Benefit.Models.Config.T_MasterGroup>();
            //if (!string.IsNullOrEmpty(usertype))//管理员登陆
            //{
            visiterGroups = db.MasterGroup.Where(a => a.MasterId == visiterID).Where(a => a.UserType == usertype).ToList();
            //}
            //else
            //{
            //    visiterGroups = db.MasterGroup.Where(a => a.MasterId == visiterID).Where(a => a.UserType == 1).ToList();
            //}
            foreach (Benefit.Models.Config.T_MasterGroup mg in visiterGroups)
            {
                int _groupId = Convert.ToInt32(mg.GroupId);
                var actionlist = db.ActionGroup.Where(a => a.GroupId == _groupId).ToList();
                foreach (Benefit.Models.Config.T_ActionGroup a in actionlist)
                {
                    if (a.ActionId.Equals("2") || a.ActionId.Equals("3"))//判断是否具有查看全部小组交易情况和公司全部交易员交易情况
                    {
                        hasPrower = true;
                        break;
                    }


                    if (("g " + groupId).Equals(a.ActionId))   //根据是否具有被查看交易员所在小组的查看权限
                    {
                        hasPrower = true;
                        break;
                    }
                }
                if (hasPrower)
                {
                    break;
                }
            }
            return hasPrower;
        }

        public bool HasCompanyPrower(int visiterID)
        {
            bool hasPrower = false;
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            int usertype = Convert.ToInt32(cookie.GetCookie("LoginUserType"));
            List<Benefit.Models.Config.T_MasterGroup> visiterGroups = new List<Benefit.Models.Config.T_MasterGroup>();
            //if (!string.IsNullOrEmpty(usertype))
            //{
            visiterGroups = db.MasterGroup.Where(a => a.MasterId == visiterID).Where(a => a.UserType == usertype).ToList();
            //}
            //else
            //{
            //    visiterGroups = db.MasterGroup.Where(a => a.MasterId == visiterID).Where(a => a.UserType == 1).ToList();
            //}
            foreach (Benefit.Models.Config.T_MasterGroup mg in visiterGroups)
            {
                int _groupId = Convert.ToInt32(mg.GroupId);
                var actionlist = db.ActionGroup.Where(a => a.GroupId == _groupId).ToList();
                foreach (Benefit.Models.Config.T_ActionGroup a in actionlist)
                {
                    if (a.ActionId.Equals("3"))//判断是否具有查看全部小组交易情况和公司全部交易员交易情况
                    {
                        hasPrower = true;
                        break;
                    }
                }
                if (hasPrower)
                {
                    break;
                }
            }
            return hasPrower;
        }

        public bool HasAdminPrower(int visiterID)
        {
            bool hasPrower = false;
            Common.Web.Cookie cookie = new Common.Web.Cookie();
            string usertype = cookie.GetCookie("LoginUserType");
            if (!string.IsNullOrEmpty(usertype) && usertype.Equals("2"))
            {
                var tradeHistorys = db.TradeHistorys.Count();
                if (tradeHistorys == 0)
                {
                    hasPrower = true;
                }
                else
                {
                    var visiterGroups = db.MasterGroup.Where(a => a.MasterId == visiterID).Where(a => a.UserType == 2).ToList();
                    foreach (Benefit.Models.Config.T_MasterGroup mg in visiterGroups)
                    {
                        int _groupId = Convert.ToInt32(mg.GroupId);
                        var actionlist = db.ActionGroup.Where(a => a.GroupId == _groupId).ToList();
                        foreach (Benefit.Models.Config.T_ActionGroup a in actionlist)
                        {
                            if (a.ActionId.Equals("5"))//判断是否具有后台管理权限
                            {
                                hasPrower = true;
                                break;
                            }
                        }
                        if (hasPrower)
                        {
                            break;
                        }
                    }
                }
            }
            return hasPrower;
        }
    }
}
